﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GPCSC.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace GPCSC.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:newsfiletable
    /// </summary>
    public partial class newsfiletable : Inewsfiletable
    {
        public newsfiletable()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid NewsfilesId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from newsfiletable");
            strSql.Append(" where NewsfilesId=@NewsfilesId ");
            SqlParameter[] parameters = {
					new SqlParameter("@NewsfilesId", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = NewsfilesId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(GPCSC.Model.newsfiletable model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into newsfiletable(");
            strSql.Append("NewsfilesId,NewsId,fileUrl,IsFirstImage,YfileName,xfileName,filetype,TJdate,IsDel)");
            strSql.Append(" values (");
            strSql.Append("@NewsfilesId,@NewsId,@fileUrl,@IsFirstImage,@YfileName,@xfileName,@filetype,@TJdate,@IsDel)");
            SqlParameter[] parameters = {
					new SqlParameter("@NewsfilesId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@NewsId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@fileUrl", SqlDbType.VarChar,100),
					new SqlParameter("@IsFirstImage", SqlDbType.Int,4),
					new SqlParameter("@YfileName", SqlDbType.VarChar,100),
					new SqlParameter("@xfileName", SqlDbType.VarChar,100),
					new SqlParameter("@filetype", SqlDbType.VarChar,50),
					new SqlParameter("@TJdate", SqlDbType.DateTime),
					new SqlParameter("@IsDel", SqlDbType.Int,4)};
            parameters[0].Value = model.NewsfilesId;
            parameters[1].Value = model.NewsId;
            parameters[2].Value = model.fileUrl;
            parameters[3].Value = model.IsFirstImage;
            parameters[4].Value = model.YfileName;
            parameters[5].Value = model.xfileName;
            parameters[6].Value = model.filetype;
            parameters[7].Value = model.TJdate;
            parameters[8].Value = model.IsDel;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GPCSC.Model.newsfiletable model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update newsfiletable set ");
            strSql.Append("NewsId=@NewsId,");
            strSql.Append("fileUrl=@fileUrl,");
            strSql.Append("IsFirstImage=@IsFirstImage,");
            strSql.Append("YfileName=@YfileName,");
            strSql.Append("xfileName=@xfileName,");
            strSql.Append("filetype=@filetype,");
            strSql.Append("TJdate=@TJdate,");
            strSql.Append("IsDel=@IsDel");
            strSql.Append(" where NewsfilesId=@NewsfilesId ");
            SqlParameter[] parameters = {
					new SqlParameter("@NewsId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@fileUrl", SqlDbType.VarChar,100),
					new SqlParameter("@IsFirstImage", SqlDbType.Int,4),
					new SqlParameter("@YfileName", SqlDbType.VarChar,100),
					new SqlParameter("@xfileName", SqlDbType.VarChar,100),
					new SqlParameter("@filetype", SqlDbType.VarChar,50),
					new SqlParameter("@TJdate", SqlDbType.DateTime),
					new SqlParameter("@IsDel", SqlDbType.Int,4),
					new SqlParameter("@NewsfilesId", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = model.NewsId;
            parameters[1].Value = model.fileUrl;
            parameters[2].Value = model.IsFirstImage;
            parameters[3].Value = model.YfileName;
            parameters[4].Value = model.xfileName;
            parameters[5].Value = model.filetype;
            parameters[6].Value = model.TJdate;
            parameters[7].Value = model.IsDel;
            parameters[8].Value = model.NewsfilesId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid NewsfilesId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from newsfiletable ");
            strSql.Append(" where NewsfilesId=@NewsfilesId ");
            SqlParameter[] parameters = {
					new SqlParameter("@NewsfilesId", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = NewsfilesId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string NewsfilesIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from newsfiletable ");
            strSql.Append(" where NewsfilesId in (" + NewsfilesIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GPCSC.Model.newsfiletable GetModel(Guid NewsfilesId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 NewsfilesId,NewsId,fileUrl,IsFirstImage,YfileName,xfileName,filetype,TJdate,IsDel from newsfiletable ");
            strSql.Append(" where NewsfilesId=@NewsfilesId ");
            SqlParameter[] parameters = {
					new SqlParameter("@NewsfilesId", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = NewsfilesId;

            GPCSC.Model.newsfiletable model = new GPCSC.Model.newsfiletable();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["NewsfilesId"] != null && ds.Tables[0].Rows[0]["NewsfilesId"].ToString() != "")
                {
                    model.NewsfilesId = new Guid(ds.Tables[0].Rows[0]["NewsfilesId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NewsId"] != null && ds.Tables[0].Rows[0]["NewsId"].ToString() != "")
                {
                    model.NewsId = new Guid(ds.Tables[0].Rows[0]["NewsId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fileUrl"] != null && ds.Tables[0].Rows[0]["fileUrl"].ToString() != "")
                {
                    model.fileUrl = ds.Tables[0].Rows[0]["fileUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsFirstImage"] != null && ds.Tables[0].Rows[0]["IsFirstImage"].ToString() != "")
                {
                    model.IsFirstImage = int.Parse(ds.Tables[0].Rows[0]["IsFirstImage"].ToString());
                }
                if (ds.Tables[0].Rows[0]["YfileName"] != null && ds.Tables[0].Rows[0]["YfileName"].ToString() != "")
                {
                    model.YfileName = ds.Tables[0].Rows[0]["YfileName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["xfileName"] != null && ds.Tables[0].Rows[0]["xfileName"].ToString() != "")
                {
                    model.xfileName = ds.Tables[0].Rows[0]["xfileName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["filetype"] != null && ds.Tables[0].Rows[0]["filetype"].ToString() != "")
                {
                    model.filetype = ds.Tables[0].Rows[0]["filetype"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TJdate"] != null && ds.Tables[0].Rows[0]["TJdate"].ToString() != "")
                {
                    model.TJdate = DateTime.Parse(ds.Tables[0].Rows[0]["TJdate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsDel"] != null && ds.Tables[0].Rows[0]["IsDel"].ToString() != "")
                {
                    model.IsDel = int.Parse(ds.Tables[0].Rows[0]["IsDel"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NewsfilesId,NewsId,fileUrl,IsFirstImage,YfileName,xfileName,filetype,TJdate,IsDel ");
            strSql.Append(" FROM newsfiletable ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" NewsfilesId,NewsId,fileUrl,IsFirstImage,YfileName,xfileName,filetype,TJdate,IsDel ");
            strSql.Append(" FROM newsfiletable ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "newsfiletable";
            parameters[1].Value = "NewsfilesId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

